#####################################################################
##Replication Code for ##############################################
##The Politics of Promotion in China's Foreign Policy Bureaucracy ###
##Tyler Jost and Yucong Li ##########################################
##The China Quarterly ###############################################
##(2) Descriptive Analysis ##########################################
#####################################################################

#########################################
####Load Data############################
#########################################

rm(list = ls())
background <- read.csv("prc_fa_leadership_background.csv")
load("mfa_personnel.RData")
load("mfa_appointments.RData")
load("descriptives.RData")
load("prc_treaties.RData")

##############################################
##Basic Descriptives #########################
##############################################

##Calculate number of unique individuals in the data
length(unique(individual$cname)) #1357 invididuals

##Calculate number of appointments in the data
nrow(d) #11296 appointments

##Calculate number of treaties in the data
nrow(t) #7066 treaties

##############################################
##Descriptive Analysis########################
##############################################
##Figure 1 Calculations#######################
##############################################

##Identify Only MFA Backgrounds
background$dip_background[background$dip_background=="Yes (MFA)"] <- "Yes"
background$dip_background[background$dip_background=="Yes (ILD)"] <- "No"

##Calculate Shares
summary <- background %>%
  group_by(organization, dip_background)  %>%
  summarise(count = n(), .groups = "drop") %>%
  group_by(organization) %>%
  mutate(share = count / sum(count))

##Identify the organizations with shares 
print(summary$organization[summary$dip_background=="Yes" & summary$share>=0.75])
print(summary$organization[summary$dip_background=="Yes" & summary$share<0.75 & summary$share>=0.25])

##MFA Backgrounds among Taiwan Affairs Office Directors
round(nrow(background[background$organization=="Taiwan Affairs Office" & background$dip_background=="Yes",])/nrow(background[background$organization=="Taiwan Affairs Office",]) ,2) #29%

##MFA Backgrounds among Hong Kong and Macau Affairs Office Directors
round(nrow(background[background$organization=="Hong Kong and Macau Affairs Office" & background$dip_background=="Yes",])/nrow(background[background$organization=="Hong Kong and Macau Affairs Office",]) ,2) #29%

##MFA Backgrounds among ILD Directors
round(nrow(background[background$organization=="International Department" & background$dip_background=="Yes",])/nrow(background[background$organization=="International Department",]) ,2) #42%

##############################################
##Descriptive Analysis########################
##############################################
##Table 1 Calculations########################
##############################################

##Group State Councillors and Vice Premiers
background$cposition[background$cposition=="副总理"] <- "国务委员/副总理"
background$cposition[background$cposition=="国务委员"] <- "国务委员/副总理"

##Group Chair/Secretary, Foreign Affairs Leading Small Group
background$cposition[background$cposition=="外事工作领导小组副组长"] <- "外事工作领导小组副组长/秘书长"
background$cposition[background$cposition=="外事工作领导小组秘书长"] <- "外事工作领导小组副组长/秘书长"

##Group Foreign Affairs Office Director
background$cposition[background$cposition=="外事工作领导小组办公室主任"] <- "外事办公室主任"
background$cposition[background$cposition=="外事工作委员会办公室主任"] <- "外事办公室主任"

##Calculate Totals/Shares

##State Councillor/Vice Premier for Foreign Affairs - Total
nrow(background[background$cposition=="国务委员/副总理",]) #9
##State Councillor/Vice Premier for Foreign Affairs - MFA Count
nrow(background[background$cposition=="国务委员/副总理" & background$dip_background=="Yes",]) #6
##State Councillor/Vice Premier for Foreign Affairs - MFA Share
round(nrow(background[background$cposition=="国务委员/副总理" & background$dip_background=="Yes",]) / nrow(background[background$cposition=="国务委员/副总理",]),2) #67%

##Deputy Chair/Secretary, Foreign Affairs Leading Small Group - Total
nrow(background[background$cposition=="外事工作领导小组副组长/秘书长",]) #6
##Deputy Chair/Secretary, Foreign Affairs Leading Small Group - MFA Count
nrow(background[background$cposition=="外事工作领导小组副组长/秘书长" & background$dip_background=="Yes",]) #5
##Deputy Chair/Secretary, Foreign Affairs Leading Small Group - MFA Share
round(nrow(background[background$cposition=="外事工作领导小组副组长/秘书长" & background$dip_background=="Yes",]) / nrow(background[background$cposition=="外事工作领导小组副组长/秘书长",]),2) #83%

##Foreign Minister - Total
nrow(background[background$cposition=="外交部长",]) #13
##Foreign Minister - MFA Count
nrow(background[background$cposition=="外交部长" & background$dip_background=="Yes",]) #10
##Foreign Minister - MFA Share
round(nrow(background[background$cposition=="外交部长" & background$dip_background=="Yes",]) / nrow(background[background$cposition=="外交部长",]),2) #77%

##Director, Foreign Affairs Office - Total
nrow(background[background$cposition=="外事办公室主任",]) #10
##Director, Foreign Affairs Office - MFA Count
nrow(background[background$cposition=="外事办公室主任" & background$dip_background=="Yes",]) #8
##Director, Foreign Affairs Office - MFA Share
round(nrow(background[background$cposition=="外事办公室主任" & background$dip_background=="Yes",]) / nrow(background[background$cposition=="外事办公室主任",]),2) #80%

##############################################
##Descriptive Analysis########################
##############################################
##Trends in MFA Composition###################
##############################################

d.stat <- descriptives %>% 
  group_by(year) %>% 
  summarise(male = mean(male, na.rm = T),
            long_march = mean(long_march, na.rm = T),
            military = mean(military, na.rm = T),
            civilcollege = mean(civilcollege, na.rm = T),
            abroad = mean(abroad, na.rm = T),
            ild = mean(ild, na.rm = T))
d.stat <- as.data.frame(d.stat)

df <- melt(d.stat, id.vars = 'year', variable.name = 'series')
levels(df$series) <- list("Male" = "male",       
                          "Long March" = "long_march",
                          "Military" = "military",
                          "College" = "civilcollege",
                          "Experience Abroad" = "abroad",
                          "ILD" = "ild")

plot1 <- ggplot(df, aes(year, value)) +
  geom_line(aes(colour = series, linetype = series)) + 
  theme_minimal() + xlab("") + ylab("Share of Senior Chinese Diplomats") + 
  theme(legend.position="bottom", legend.margin=margin(-12.5,0,0,0), legend.text = element_text(face="bold"),
        axis.title.y = element_text(face="bold"), legend.title=element_blank(), 
        axis.text=element_text(face="bold"))

ggsave("figure_2.pdf", plot = plot1, scale = 1, width = 6, height = 4, dpi = 500, limitsize = T)

##############################################
##Descriptive Analysis########################
##############################################
##Trends in MFA Education#####################
##############################################

d.stat2 <- descriptives %>% 
  group_by(year) %>% 
  summarise(arts_major = mean(arts_major, na.rm = T),
            language_major = mean(language_major, na.rm = T),
            science_major = mean(science_major, na.rm = T),
            socialscience_major = mean(socialscience_major, na.rm = T))
d.stat2 <- as.data.frame(d.stat2)

df2 <- melt(d.stat2, id.vars = 'year', variable.name = 'series')
levels(df2$series) <- list("Arts" = "arts_major",       
                           "Language" = "language_major",
                           "Science" = "science_major",
                           "Social Science" = "socialscience_major")

plot2 <- ggplot(df2, aes(year, value)) +
  geom_line(aes(colour = series, linetype = series)) + 
  theme_minimal() + xlab("") + ylab("% of Senior MFA Cadres") + 
  theme(legend.position="bottom", legend.margin=margin(-12.5,0,0,0), legend.text = element_text(face="bold"),
        axis.title.y = element_text(face="bold"), legend.title=element_blank(), 
        axis.text=element_text(face="bold"))

##Create Appendix Figure 1
ggsave("majors.pdf", plot = plot2, scale = 1, width = 6, height = 4, dpi = 500, limitsize = T)


##############################################
##Descriptive Analysis########################
##############################################
##Calculations################################
##############################################

earlymao <- c(1949:1962)
latemao <- c(1963:1976)
postmao <- c(1977:2023)

#Military Status during Early Mao Era
round(min(df$value[df$series=="Military" & df$year %in% earlymao]),2)*100 #67%
round(max(df$value[df$series=="Military" & df$year %in% earlymao]),2)*100 #84%

#ILD Status during Early Mao Era
round(min(df$value[df$series=="ILD" & df$year %in% earlymao]),2)*100 #4%
round(max(df$value[df$series=="ILD" & df$year %in% earlymao]),2)*100 #11%

#Education during Early Mao Era
round(df$value[df$series=="College" & df$year==1950],2)*100 #48%
round(df$value[df$series=="College" & df$year==1962],2)*100 #64%

#Time Abroad during Early Mao Era
round(max(df$value[df$series=="Experience Abroad" & df$year %in% earlymao]),2)*100 #67%

#Long march during Early Mao Era
round(df$value[df$series=="Long March" & df$year==1950],2)*100 #52%
round(df$value[df$series=="Long March" & df$year==1962],2)*100 #20%

#Gender during Late Mao Era
round(mean(df$value[df$series=="Male" & df$year %in% latemao]),2)*100 #99%

#Military Status during Late Mao Era
round(mean(df$value[df$series=="Military" & df$year %in% latemao]),2)*100 #81%

#Education during Late Mao Era
round(df$value[df$series=="College" & df$year==1963],2)*100 #63%
round(df$value[df$series=="College" & df$year==1979],2)*100 #39%


#Military Status during the Reform Era
round(min(df$value[df$series=="Military" & df$year==1982]),2)*100 #66%
round(max(df$value[df$series=="Military" & df$year==1998]),2)*100 #1%

#ILD Status during the Reform Era
round(mean(df$value[df$series=="ILD" &  df$year>=1982]),2)*100 #4%

#Education during the Reform Era
round(df$value[df$series=="College" & df$year==1982],2)*100 #42%
round(df$value[df$series=="College" & df$year==2003],2)*100 #96%

#Language specialization during the Reform Era
round(df2$value[df2$series=="Language" & df2$year==1982],2)*100 #22%
round(df2$value[df2$series=="Language" & df2$year==2003],2)*100 #88%

#Gender Status during the Reform Era
round(1-min(df$value[df$series=="Male" & df$year==1982]),2)*100 #2%
round(1-max(df$value[df$series=="Male" & df$year==2022]),2)*100 #11%

##Party Gender
round(29.3/(29.3+98.04),2)*100 #23%

##Overall range of military share
round(max(df$value[df$series=="Military"]),2)*100 #89%
round(min(df$value[df$series=="Military"]),2)*100 #0%

